package com.xiaodu.acer.phoneplay.ui.activity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.xiaodu.acer.phoneplay.R;
import com.xiaodu.acer.phoneplay.bean.Subject;
import com.xiaodu.acer.phoneplay.common.rx.RxBus;
import com.xiaodu.acer.phoneplay.di.component.AppComponent;
import com.xiaodu.acer.phoneplay.ui.base.BaseActivity;
import com.xiaodu.acer.phoneplay.ui.fragment.SubjectDetailFragment;
import com.xiaodu.acer.phoneplay.ui.fragment.SubjectFragment;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * 热门主题
 */
public class SubjectActivity extends BaseActivity {

    public static final int FRAGMENT_SUBJECT = 0;
    public static final int FRAGMENT_DETAIL = 1;

    private int fragmentIndex = FRAGMENT_SUBJECT;

    private FragmentManager mFragmentManager;


    //主题列表
    SubjectFragment mSubjectFragment;

    //主题详情页面
    SubjectDetailFragment mSubjectDetailFragment;


    @Override
    public int setLayout() {
        return R.layout.template_toolbar_framelayout;
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

    @Override
    public void initView() {

        mFragmentManager = getSupportFragmentManager();

        showSubjectFragment();

        showSubjectDetailFragmentRxBus();
    }

    private void showSubjectDetailFragmentRxBus() {
        RxBus.getDefault()
                .toObservable(Subject.class)
                .subscribe(new Consumer<Subject>() {
                    @Override
                    public void accept(@NonNull Subject subject) throws Exception {
                        showSubjectDetailFragment(subject);
                    }
                });
    }

    /**
     * 显示详情页面
     *
     * @param subject
     */
    private void showSubjectDetailFragment(Subject subject) {
        fragmentIndex = FRAGMENT_DETAIL;
        FragmentTransaction ft = mFragmentManager.beginTransaction();

        if (mSubjectDetailFragment != null) {
            ft.remove(mSubjectDetailFragment);
        }

        mSubjectDetailFragment = new SubjectDetailFragment(subject);
        ft.add(R.id.content, mSubjectDetailFragment);
        ft.commit();

        mToolBar.setTitle(subject.getTitle());

    }

    /**
     * commitAllowingStateLoss
     * 显示主题列表页面
     */
    private void showSubjectFragment() {
        fragmentIndex = FRAGMENT_SUBJECT;
        mToolBar.setTitle("主题");
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        hideFragment(ft);

        if (mSubjectFragment == null) {
            mSubjectFragment = new SubjectFragment();
            ft.add(R.id.content, mSubjectFragment);
        } else {
            ft.show(mSubjectFragment);
        }

        ft.commit();

    }

    private void hideFragment(FragmentTransaction ft) {
        if (mSubjectFragment != null) {
            ft.hide(mSubjectFragment);
        }
        if (mSubjectDetailFragment != null) {
            ft.hide(mSubjectDetailFragment);
        }


    }

    private void handNavigation() {
        if (fragmentIndex == FRAGMENT_SUBJECT) {
            finish();
        } else {
            showSubjectFragment();
        }

    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handNavigation();
            }

        });

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        handNavigation();
    }


}
